SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO



CREATE PROC [dbo].[SP_TXMVTO_CNTB_Q22]  
/*-------------------------------------------------------------------*/  
/*----- Empresa           : OFISIS S.A.                          ----*/  
/*----- Cliente           : OFISIS S.A.                          ----*/  
/*----- Sistema           : Contabilidad                         ----*/  
/*----- Modulo            : Empresa                              ----*/  
/*----- Programa          : Registro de Compras (COA)            ----*/  
/*----- Nombre            : SP_TXMVTO_CNTB_Q22                   ----*/  
/*----- Script            : TXMVTO_CNTB                          ----*/  
/*----- Desarrollado por  : Franco Gamboni Koifman               ----*/  
/*----- Fecha             : 23/06/1997                           ----*/  
/*----- Base de Datos     : MS SQL Server                        ----*/  
/*----- Version BD        : 6.5                                  ----*/  
/*-------------------------------------------------------------------*/  
/*------MODIF.            : PDGV                                 ----*/  
/*----- FECHA MODIF       :10/05/2004                            ----*/  
/*----- DETALLE MODIF     : SE QUITO JOIN CON ESTADO 'ACT'       ----*/
/*-------------------------------------------------------------------*/  
/*-------------------------------------------------------------------*/  
/*----- Drop proc SP_TXMVTO_CNTB_Q22                             ----*/  
/*----- Grant all on SP_TXMVTO_CNTB_Q22 to PUBLIC                ----*/  
/*----- SP_TXMVTO_CNTB_Q22 '01', 1, 2000, 1                      ----*/  
/*-------------------------------------------------------------------*/  
  
@ISCO_EMPR   TD_VC_002,  
@INNU_CNTB_EMPR  TD_IN_001_CONT,  
@INNU_ANNO   TD_IN_004_ANNO,  
@INNU_MESE   TD_IN_002_MESE  
  
As  
  
Create Table #TWMVTO_CNTB_Q22_1  
(FE_DOCU  datetime  NULL,  
 TI_DOCU_SUNA char(02)   NULL,  
 TI_DOCU_CNTB char(03)   NULL,  
 NU_DOCU char(20)  NULL,  
 CO_UNID_RGST char(03)    NULL,  
 CO_OPRC char(03)   NULL,  
 NS_ASTO char(10)  NULL,  
 CO_PROV varchar(20)  NULL,  
 NO_CORT_PROV char(50)  NULL,  
 NU_RUCS_PROV varchar(20)  NULL,  
 CO_MONE char(03)   NULL,  
 IM_INAF_ORIG Numeric(16,4)    NULL,  
 IM_AFEC_ORIG Numeric(16,4)     NULL,       
 IM_IIGV_ORIG Numeric(16,4)    NULL,  
 IM_FONA_ORIG Numeric(16,4)     NULL,  
 IM_RENT_ORIG Numeric(16,4)     NULL,  
 IM_TOTA_ORIG Numeric(16,4)     NULL,  
 IM_INAF_SOLE Numeric(16,4)    NULL,  
 IM_AFEC_SOLE Numeric(16,4)     NULL,  
 IM_IIGV_SOLE Numeric(16,4)    NULL,  
 IM_FONA_SOLE Numeric(16,4)     NULL,  
 IM_RENT_SOLE Numeric(16,4)     NULL,  
 IM_TOTA_SOLE Numeric(16,4)     NULL,  
 FE_DOCU_REFE   datetime        NULL,  
 TI_DOCU_REFE   char(03)        NULL,  
 NU_DOCU_REFE   char(20)        NULL,  
 FA_CAMB Numeric(15,6)    NULL)  
   
Declare   
/*------------------------------*/  
@CSCO_PROV          TD_VC_020,  
@CSCO_OPRC          TD_VC_003,  
@CSNS_ASTO          TD_VC_010,  
@CSNO_CORT_PROV     TD_VC_050,  
@VSNU_RUCS_PROV     TD_VC_020,  
@CSNU_RUCS          TD_VC_020,  
@CSNU_RUCS_PROV     TD_VC_020,  
@CSCO_MONE          TD_VC_008,  
@CNIM_INAF_ORIG     TD_NU_016_004,  
@CNIM_AFEC_ORIG     TD_NU_016_004,  
@CNIM_IIGV_ORIG     TD_NU_016_004,  
@CNIM_FONA_ORIG     TD_NU_016_004,   
@CNIM_RENT_ORIG     TD_NU_016_004,  
@CNIM_TOTA_ORIG     TD_NU_016_004,  
@CNIM_INAF_SOLE     TD_NU_016_004,  
@CNIM_AFEC_SOLE     TD_NU_016_004,   
@CNIM_IIGV_SOLE     TD_NU_016_004,  
@CNIM_FONA_SOLE     TD_NU_016_004,  
@CNIM_RENT_SOLE     TD_NU_016_004,  
@CNIM_TOTA_SOLE     TD_NU_016_004,  
@VNIM_AFEC_SOLE     TD_NU_016_004,  
@VNIM_IIGV_SOLE     TD_NU_016_004,  
@VSNU_ORDE          TD_VC_010,  
@VSDE_MESE          varchar(06),  
@VSDE_ANNO          varchar(06),  
@VSDE_PERI          varchar(06),  
@VNNU_ANNO_DOCU     TD_IN_001,  
@VNNU_MESE_DOCU     TD_IN_001,  
@VNNU_DIAS_DOCU     TD_IN_001,  
@VSDE_ANNO_DOCU     varchar(06),  
@VSDE_MESE_DOCU     varchar(06),  
@VSDE_DIAS_DOCU     varchar(06),  
@VSDE_FECH_DOCU     TD_VC_010,  
@VSNU_SERI          varchar(06),  
@VNNU_POSI_SERI     TD_IN_001,  
@VNNU_ANNO_REFE     TD_IN_001,  
@VNNU_MESE_REFE     TD_IN_001,  
@VNNU_DIAS_REFE     TD_IN_001,  
@VSDE_ANNO_REFE     varchar(06),  
@VSDE_MESE_REFE     varchar(06),  
@VSDE_DIAS_REFE     varchar(06),  
@VNNU_POSI_REFE     TD_IN_001,  
@VSNU_SERI_REFE     varchar(06),  
@VSDE_FECH_REFE     TD_VC_010,  
@VSTI_SUNA_REFE     TD_VC_003,  
@VNNU_SECU_FACT     int,  
@VNNU_SECU_NOTA     int,  
@VNNU_SECU_POLI     int,  
@VNNU_SECU_PROV     int,  
@VSDE_CODI_ADQU     TD_VC_003,  
/*------------------------------*/  
@VSNS_ASTO_BASE     char(16),  
@VSCO_MONE_BASE     TD_VC_003,  
@VSCO_MONE_BAS1     TD_VC_003,  
@VNIM_TOTA_BASX     TD_NU_016_004,  
@VNIM_TOTA_BASY     TD_NU_016_004,  
@VDFE_DOCU     TD_DT_001,  
@VSTI_DOCU_CNTB     TD_VC_003,  
@VSNU_DOCU     TD_VC_020,  
@VSCO_UNID     TD_VC_003,  
@VSCO_OPRC     TD_VC_003,  
@VSNS_ASTO     TD_VC_010,  
@VSCO_PROV     TD_VC_020,  
@VSNO_CORT_PROV     TD_VC_050,  
@VSCO_MONE     TD_VC_003,  
@VNIM_INAF_ORIX     TD_NU_016_004,  
@VNIM_AFEC_ORIX     TD_NU_016_004,  
@VNIM_IIGV_ORIX     TD_NU_016_004,  
@VNIM_FONA_ORIX     TD_NU_016_004,  
@VNIM_RENT_ORIX     TD_NU_016_004,  
@VNIM_TOTA_ORIX     TD_NU_016_004,  
@VNIM_INAF_SOLX     TD_NU_016_004,  
@VNIM_AFEC_SOLX     TD_NU_016_004,  
@VNIM_IIGV_SOLX     TD_NU_016_004,  
@VNIM_FONA_SOLX     TD_NU_016_004,  
@VNIM_RENT_SOLX     TD_NU_016_004,  
@VNIM_TOTA_SOLX     TD_NU_016_004,  
@VNFA_CAMB     TD_NU_009_004,  
@VNIM_IIGV_ORIG     TD_NU_016_004,  
@VNIM_FONA_ORIG     TD_NU_016_004,  
@VNIM_RENT_ORIG     TD_NU_016_004,  
@VNIM_TOTA_ORIG     TD_NU_016_004,  
@VNIM_FONA_SOLE     TD_NU_016_004,  
@VNIM_RENT_SOLE     TD_NU_016_004,  
@VNIM_TOTA_SOLE     TD_NU_016_004,  
@VNPO_IGV1     TD_NU_009_004,  
@VNPO_IGV2     TD_NU_009_004,  
@VNPO_REN1     TD_NU_009_004,  
@VNPO_REN2     TD_NU_009_004,  
@VSCO_OPR1     TD_VC_003,  
@VSCO_OPR2     TD_VC_003,  
@VSID_COMP      TD_VC_003,  
@VSID_IIGV      TD_VC_003,  
@VSID_FONA      TD_VC_003,  
@VSID_RENT      TD_VC_003,  
@VSTI_DOCU_AUXI     char(06),  
@CSTI_DOCU_SUNA     TD_VC_003,  
@VSCO_SUNA        TD_VC_002,  
@CSID_CNTA      TD_VC_003,  
@CDFE_DOCU     TD_DT_001,  
@CSTI_DOCU_CNTB     TD_VC_003,  
@CSNU_DOCU     TD_VC_020,  
@CSCO_UNID     TD_VC_003,  
@CSCO_AUXI     TD_VC_020,  
@CSCN_CNTB_EMPR     TD_VC_010,  
@CSTI_OPER_CNTB     TD_VC_001,  
@CSCO_UNID_RGST     TD_VC_003,  
@CSNU_ASTO          TD_VC_010,  
@CNFA_CAMB     TD_NU_015_006,  
@CNIM_MVTO_ORIG     TD_NU_016_004,  
@CNIM_MVTO_CNTB     TD_NU_016_004,  
@CDFE_DOCU_REFE     TD_DT_001,  
@CSTI_DOCU_REFE     TD_VC_003,  
@CSNU_DOCU_REFE     TD_VC_020,  
@VDFE_DOCU_REFE     TD_DT_001,  
@VSTI_DOCU_REFE     TD_VC_003,  
@VSNU_DOCU_REFE     TD_VC_020  
  
  
/*----- Inicializando las variables -----*/  
Select @VNIM_IIGV_ORIG = 0.00  
Select @VNIM_FONA_ORIG = 0.00  
Select @VNIM_RENT_ORIG = 0.00  
  
Select @VNIM_TOTA_ORIG = 0.00  
Select @VDFE_DOCU      = Null  
Select @VSTI_DOCU_CNTB = Null  
Select @VSNS_ASTO      = Null  
Select @VSCO_PROV      = Null  
Select @VSNO_CORT_PROV = Null  
Select @VSNU_RUCS_PROV = Null  
Select @VSCO_MONE      = Null  
Select @VNIM_AFEC_ORIX = 0.00  
Select @VNIM_INAF_ORIX = 0.00  
Select @VNIM_IIGV_ORIX = 0.00  
Select @VNIM_FONA_ORIX = 0.00  
Select @VNIM_RENT_ORIX = 0.00  
Select @VNIM_TOTA_ORIX = 0.00  
Select @VNIM_AFEC_SOLX = 0.00  
Select @VNIM_INAF_SOLX = 0.00  
Select @VNIM_IIGV_SOLX = 0.00  
Select @VNIM_FONA_SOLX = 0.00  
Select @VNIM_RENT_SOLX = 0.00  
Select @VNIM_TOTA_ORIX = 0.00  
Select @VNFA_CAMB      = 0.00      
  
Select @VNNU_SECU_FACT = 0   
Select @VNNU_SECU_NOTA = 0   
Select @VNNU_SECU_POLI = 0   
Select @VNNU_SECU_PROV = 0   
  
  
/*----- Obteniendo datos de Nro. de Contabilidad -----*/  
Select   
 @VSCO_MONE_BAS1 = CO_MONE,  
 @VSID_COMP   = ID_COMP,   
 @VSID_IIGV    = ID_IMPT_VENT,   
 @VSID_FONA   = ID_FONA,   
 @VSID_RENT   = ID_IMPT_RENT  
From TTCNTB_EMPR  
Where   
 CO_EMPR = @ISCO_EMPR and  
 NU_CNTB_EMPR = @INNU_CNTB_EMPR  
  
Select    
    @VNPO_REN1  = PO_IMPT_REN1,  
    @VNPO_REN2  = PO_IMPT_REN2,  
    @VNPO_IGV1   = PO_IMPT_VEN1,   
    @VNPO_IGV2   = PO_IMPT_VEN2   
    From TTPORC_IMPT  
    Where CO_EMPR = @ISCO_EMPR  
    And NU_ANNO = @INNU_ANNO  
    And NU_MESE = @INNU_MESE  
  
If @VNPO_IGV1 IS Null  
 Select @VNPO_IGV1 = 0.00  
If @VNPO_IGV2 IS Null  
 Select @VNPO_IGV2 = 0.00  
If @VNPO_REN1 IS Null  
 Select @VNPO_REN1 = 0.00  
If @VNPO_REN2 IS Null  
 Select @VNPO_REN2 = 0.00  
  
  
/*----- Cursor para el reporte de Registro de Compras -----*/  
Declare CU_TXMVTO_CNTB_Q22_1 Cursor  
 For  
  Select t1.FE_DOCU, t1.TI_DOCU, t1.NU_DOCU, t1.CO_UNID_CNTB, t1.CO_OPRC_CNTB,   
         t1.NU_ASTO, t1.CO_AUXI_EMPR, t1.CO_CNTA_EMPR, t1.TI_OPER, t1.CO_MONE,   
         t1.FA_CAMB, t1.IM_MVTO_ORIG, t1.IM_MVTO_CNTB, t1.FE_DOCU_REFE,   
         t1.TI_DOCU_REFE, t1.NU_DOCU_REFE, t2.ID_CNTA  
   From TXMVTO_CNTB t1, TTIDEN_CNTA t2, TMOPRC_CNTB t3, TMPLAN_EMPR t4  
   Where   t1.CO_EMPR = @ISCO_EMPR   
    and t1.NU_CNTB_EMPR = @INNU_CNTB_EMPR  
    and t1.CO_OPRC_CNTB in (Select CO_OPRC_CNTB From TTIDEN_OPER Where CO_EMPR =  
                                    @ISCO_EMPR And ID_OPRC = @VSID_COMP And  
                                    NU_CNTB_EMPR = @INNU_CNTB_EMPR)  
    and t1.CO_CNTA_EMPR in (Select CO_CNTA_EMPR From TTIDEN_CNTA Where CO_EMPR =  
                          @ISCO_EMPR And ID_CNTA = @VSID_COMP or ID_CNTA = @VSID_IIGV And  
                          NU_CNTB_EMPR = @INNU_CNTB_EMPR)  
    and t1.NU_ANNO = @INNU_ANNO   
    and t1.NU_MESE = @INNU_MESE  
    and T1.SI_MVTO_CNTB = 'APR'  
    and t2.CO_EMPR  = t1.CO_EMPR  
    and t2.CO_CNTA_EMPR = t1.CO_CNTA_EMPR  
    and t2.NU_CNTB_EMPR = t1.NU_CNTB_EMPR  
    And t1.CO_EMPR = t3.CO_EMPR  
    And t1.CO_OPRC_CNTB = t3.CO_OPRC_CNTB  
--    And t3.TI_SITU = 'ACT'  
    And t4.CO_EMPR = t1.CO_EMPR  
    And t4.NU_ANNO = 9999  
    And t4.CO_CNTA_EMPR = t1.CO_CNTA_EMPR  
--    And t4.NU_ANNO In (Select Max(NU_ANNO) From TMPLAN_EMPR Where CO_EMPR = @ISCO_EMPR and NU_ANNO = @INNU_ANNO)  
--    And t4.TI_SITU = 'ACT'  
  
   Order By t1.CO_UNID_CNTB, t1.CO_OPRC_CNTB, t1.NU_ASTO, t1.CO_CNTA_EMPR desc,  
            t1.TI_AUXI_EMPR, t1.CO_AUXI_EMPR  
  
 Open CU_TXMVTO_CNTB_Q22_1  
 Fetch CU_TXMVTO_CNTB_Q22_1 into  
    @CDFE_DOCU, @CSTI_DOCU_CNTB,  @CSNU_DOCU, @CSCO_UNID, @CSCO_OPRC, @CSNS_ASTO,  
    @CSCO_AUXI, @CSCN_CNTB_EMPR, @CSTI_OPER_CNTB, @CSCO_MONE, @CNFA_CAMB,  @CNIM_MVTO_ORIG,  
    @CNIM_MVTO_CNTB, @CDFE_DOCU_REFE, @CSTI_DOCU_REFE, @CSNU_DOCU_REFE, @CSID_CNTA  
  
Select @VSNS_ASTO_BASE = Null  
Select @VSCO_MONE_BASE = Null  
  
/*-------- Acumula Movimientos -----*/  
While @@Fetch_status = 0  
Begin  
/*----- Actualiza Temporal -----*/  
If @VSNS_ASTO_BASE != Rtrim(@CSCO_UNID) + Rtrim(@CSCO_OPRC) + Rtrim(@CSNS_ASTO)  
    And @VSCO_MONE_BASE IS NOT Null  
  
Begin  
  Select @VNIM_TOTA_BASX = Round(@VNIM_TOTA_ORIG - @VNIM_IIGV_ORIG,2)  
  Select @VNIM_TOTA_BASY = Round(@VNIM_IIGV_ORIG / ((@VNPO_IGV1 + @VNPO_IGV2) / 100), 2)  
  If Abs(Round(@VNIM_TOTA_BASX - @VNIM_TOTA_BASY,2)) > 0.50  
   Begin  
     Select @VNIM_AFEC_ORIX = Round(@VNIM_TOTA_BASY,2)  
     Select @VNIM_INAF_ORIX = Round(@VNIM_TOTA_BASX - @VNIM_TOTA_BASY,2)  
   End      
  Else  
   Begin  
     Select @VNIM_AFEC_ORIX = Round(@VNIM_TOTA_BASX,2)  
     Select @VNIM_INAF_ORIX = 0.00  
   End      
  If @VSCO_MONE != @VSCO_MONE_BAS1  
   Begin  
     Select @VNIM_IIGV_ORIX = @VNIM_IIGV_ORIG  
     Select @VNIM_FONA_ORIX = @VNIM_FONA_ORIG  
     Select @VNIM_RENT_ORIX = @VNIM_RENT_ORIG  
     Select @VNIM_TOTA_ORIX = @VNIM_TOTA_ORIG + (@VNIM_FONA_ORIX + @VNIM_RENT_ORIX)  
     Select @VNIM_AFEC_SOLX = Round(@VNIM_AFEC_ORIX * @VNFA_CAMB,2)  
     Select @VNIM_INAF_SOLX = Round(@VNIM_INAF_ORIX * @VNFA_CAMB,2)  
     Select @VNIM_IIGV_SOLX = Round(@VNIM_IIGV_ORIG * @VNFA_CAMB,2)  
     Select @VNIM_FONA_SOLX = Round(@VNIM_FONA_ORIG * @VNFA_CAMB,2)  
     Select @VNIM_RENT_SOLX = Round(@VNIM_RENT_ORIG * @VNFA_CAMB,2)  
     Select @VNIM_TOTA_SOLX = Round((@VNIM_TOTA_ORIG + (@VNIM_FONA_ORIX + @VNIM_RENT_ORIX))* @VNFA_CAMB,2)  
   End  
  Else  
   Begin  
     Select @VNIM_AFEC_SOLX = Round(@VNIM_AFEC_ORIX,2)  
     Select @VNIM_INAF_SOLX = Round(@VNIM_INAF_ORIX,2)      Select @VNIM_IIGV_SOLX = Round(@VNIM_IIGV_ORIG,2)  
     Select @VNIM_FONA_SOLX = Round(@VNIM_FONA_ORIG,2)  
     Select @VNIM_RENT_SOLX = Round(@VNIM_RENT_ORIG,2)  
  
     Select @VNIM_TOTA_SOLX = Round((@VNIM_TOTA_ORIG + (@VNIM_FONA_ORIX +@VNIM_RENT_ORIX)),2)  
     Select @VNIM_AFEC_ORIX = Null  
     Select @VNIM_INAF_ORIX = Null  
     Select @VNIM_IIGV_ORIX = Null  
     Select @VNIM_FONA_ORIX = Null  
     Select @VNIM_RENT_ORIX = Null  
     Select @VNIM_TOTA_ORIX = Null  
     Select @VNFA_CAMB = Null  
   End  
  Select @VSCO_SUNA = CO_SUNA From TTDOCU_CNTB  
    Where TI_DOCU = @VSTI_DOCU_CNTB  
   
  Select @VSTI_DOCU_AUXI = @VSTI_DOCU_CNTB  
   Insert Into #TWMVTO_CNTB_Q22_1 Values  
     (@VDFE_DOCU, @VSCO_SUNA, @VSTI_DOCU_AUXI, @VSNU_DOCU, @VSCO_UNID,  
      @VSCO_OPRC,@VSNS_ASTO, @VSCO_PROV, @VSNO_CORT_PROV, @VSNU_RUCS_PROV, @VSCO_MONE,  
      @VNIM_INAF_ORIX, @VNIM_AFEC_ORIX, @VNIM_IIGV_ORIX, @VNIM_FONA_ORIX,  
      @VNIM_RENT_ORIX, @VNIM_TOTA_ORIX, @VNIM_INAF_SOLX, @VNIM_AFEC_SOLX,   
      @VNIM_IIGV_SOLX, @VNIM_FONA_SOLX, @VNIM_RENT_SOLX, @VNIM_TOTA_SOLX,  
      @VDFE_DOCU_REFE, @VSTI_DOCU_REFE, @VSNU_DOCU_REFE, @VNFA_CAMB)  
   Select @VSCO_MONE_BASE = Null  
   Select @VDFE_DOCU = Null  
   Select @VSTI_DOCU_CNTB = Null  
  
  
   Select @VSNS_ASTO = Null  
   Select @VSCO_PROV = Null  
   Select @VSNO_CORT_PROV = Null  
   Select @VSNU_RUCS_PROV  = Null  
   Select @VSCO_MONE = Null  
   Select @VNIM_IIGV_ORIG = 0.00  
   Select @VNIM_FONA_ORIG = 0.00  
   Select @VNIM_RENT_ORIG = 0.00  
   Select @VNIM_TOTA_ORIG = 0.00  
   Select @VNIM_AFEC_ORIX = 0.00  
   Select @VNIM_INAF_ORIX = 0.00  
  
   Select @VNIM_IIGV_ORIX = 0.00  
   Select @VNIM_FONA_ORIX = 0.00  
   Select @VNIM_RENT_ORIX = 0.00  
   Select @VNIM_TOTA_ORIX = 0.00  
   Select @VNIM_AFEC_SOLX = 0.00  
   Select @VNIM_INAF_SOLX = 0.00  
   Select @VNIM_IIGV_SOLX = 0.00  
   Select @VNIM_FONA_SOLX = 0.00     
   Select @VNIM_RENT_SOLX = 0.00  
   Select @VNIM_TOTA_ORIX = 0.00  
   Select @VNFA_CAMB = 0.00      
 End  
/*-----  Acumula Movimientos - Provisiones -----*/  
Select @VSNS_ASTO_BASE = Rtrim(@CSCO_UNID) + Rtrim(@CSCO_OPRC) + RTrim(@CSNS_ASTO)  
 If @CSID_CNTA = '001'  
  Begin  
    If @VSCO_MONE_BASE IS Null  
      Begin  
        Select @VSCO_PROV = CO_AUXI_EMPR,  
               @VSNO_CORT_PROV = NO_AUXI,    
               @VSNU_RUCS_PROV = NU_RUCS  
          From TMAUXI_EMPR  
          Where CO_EMPR = @ISCO_EMPR And  
                TI_AUXI_EMPR = 'P' And  
                CO_AUXI_EMPR = @CSCO_AUXI  
        If @@rowcount = 0  
  
         Begin  
           Select @VDFE_DOCU = Null  
           Select @VSTI_DOCU_CNTB = Null  
           Select @VSNU_DOCU = Null  
           Select @VSCO_PROV = Null   
           Select @VSNO_CORT_PROV = Null  
           Select @VSNU_RUCS_PROV = Null  
           Select @VDFE_DOCU_REFE = Null  
           Select @VSTI_DOCU_REFE = Null  
           Select @VSNU_DOCU_REFE = Null  
         End  
        Else   
         Begin  
           Select @VDFE_DOCU = @CDFE_DOCU  
           Select @VSTI_DOCU_CNTB = @CSTI_DOCU_CNTB  
           Select @VSNU_DOCU = @CSNU_DOCU  
           Select @VDFE_DOCU_REFE = @CDFE_DOCU_REFE  
           Select @VSTI_DOCU_REFE = @CSTI_DOCU_REFE  
           Select @VSNU_DOCU_REFE = @CSNU_DOCU_REFE  
         End  
        Select @VSCO_UNID = @CSCO_UNID   
        Select @VSCO_OPRC = @CSCO_OPRC  
        Select @VSNS_ASTO = @CSNS_ASTO  
        Select @VSCO_MONE = @CSCO_MONE  
        Select @VNFA_CAMB = Round(@CNFA_CAMB,3)  
        Select @VSCO_MONE_BASE = @CSCO_MONE  
      End  
    
     If @VSCO_MONE_BASE IS NOT Null  
      Begin  
        If @VSCO_MONE_BASE != @CSCO_MONE  
         Begin  
           Select @CNIM_MVTO_ORIG = Round(@CNIM_MVTO_ORIG / @VNFA_CAMB,2)  
         End  
       If @CSTI_OPER_CNTB = 'ABO'  
        Begin  
           Select @VNIM_TOTA_ORIG = Round(@VNIM_TOTA_ORIG + @CNIM_MVTO_ORIG,2)  
        End  
       Else If @CSTI_OPER_CNTB = 'CAR'  
        Begin  
  
           Select @VNIM_TOTA_ORIG = Round(@VNIM_TOTA_ORIG - @CNIM_MVTO_ORIG,2)  
        End  
     End  
    End  
   /*-----  Acumula Movimientos - IGV  -----*/  
   If @CSID_CNTA = '009'  
    Begin  
     If @VSCO_MONE_BASE IS NOT Null  
      Begin  
       If @VSCO_MONE_BASE != @CSCO_MONE  
        Begin  
         Select @CNIM_MVTO_ORIG = Round(@CNIM_MVTO_ORIG / @VNFA_CAMB,2)  
        End  
If @CSTI_OPER_CNTB = 'CAR'  
        Begin  
         Select @VNIM_IIGV_ORIG = Round(@VNIM_IIGV_ORIG + @CNIM_MVTO_ORIG,2)  
        End  
       Else If @CSTI_OPER_CNTB = 'ABO'  
        Begin  
         Select @VNIM_IIGV_ORIG = Round(@VNIM_IIGV_ORIG - @CNIM_MVTO_ORIG,2)  
        End  
  End  
    End  
   
   /*-----  Acumula Movimientos - FONAVI  -----*/  
   If @CSID_CNTA = '008'  
    Begin  
 If @VSCO_MONE_BASE IS NOT Null  
      Begin  
       If @VSCO_MONE_BASE != @CSCO_MONE  
        Select @CNIM_MVTO_ORIG = Round(@CNIM_MVTO_ORIG / @VNFA_CAMB,2)  
       If @CSTI_OPER_CNTB = 'CAR'  
        Select @VNIM_FONA_ORIG = Round(@VNIM_FONA_ORIG + @CNIM_MVTO_ORIG,2)  
       Else If @CSTI_OPER_CNTB = 'ABO'  
        Select @VNIM_FONA_ORIG = Round(@VNIM_FONA_ORIG - @CNIM_MVTO_ORIG,2)  
      End  
    End  
   
   /*-----   Acumula Movimientos - 4ta. CAT.   -----*/  
   If @CSID_CNTA = '010'  
    Begin  
     If @VSCO_MONE_BASE IS NOT Null  
      Begin  
       If @VSCO_MONE_BASE != @CSCO_MONE  
        Begin  
         Select @CNIM_MVTO_ORIG = Round(@CNIM_MVTO_ORIG / @VNFA_CAMB,2)  
  
        End  
       If @CSTI_OPER_CNTB = 'CAR'  
        Begin  
         Select @VNIM_RENT_ORIG = Round(@VNIM_RENT_ORIG + @CNIM_MVTO_ORIG,2)  
        End  
       Else If @CSTI_OPER_CNTB = 'ABO'  
        Begin  
         Select @VNIM_RENT_ORIG = Round(@VNIM_RENT_ORIG - @CNIM_MVTO_ORIG,2)  
        End  
      End  
    End  
 Fetch CU_TXMVTO_CNTB_Q22_1 into  
    @CDFE_DOCU, @CSTI_DOCU_CNTB,  @CSNU_DOCU, @CSCO_UNID, @CSCO_OPRC, @CSNS_ASTO,  
    @CSCO_AUXI, @CSCN_CNTB_EMPR, @CSTI_OPER_CNTB, @CSCO_MONE, @CNFA_CAMB,  @CNIM_MVTO_ORIG,  
    @CNIM_MVTO_CNTB, @CDFE_DOCU_REFE, @CSTI_DOCU_REFE, @CSNU_DOCU_REFE, @CSID_CNTA  
 End  
 Close CU_TXMVTO_CNTB_Q22_1  
 Deallocate CU_TXMVTO_CNTB_Q22_1  
  
If  @VSCO_MONE_BASE IS NOT Null   
 Begin  
   Select @VNIM_TOTA_BASX = Round(@VNIM_TOTA_ORIG - @VNIM_IIGV_ORIG,2)  
   Select @VNIM_TOTA_BASY = Round(@VNIM_IIGV_ORIG / ((@VNPO_IGV1 + @VNPO_IGV2) / 100),2)  
  If Abs(Round(@VNIM_TOTA_BASX - @VNIM_TOTA_BASY,2)) > 0.50  
   Begin  
     Select @VNIM_AFEC_ORIX = Round(@VNIM_TOTA_BASY,2)  
     Select @VNIM_INAF_ORIX = Round(@VNIM_TOTA_BASX - @VNIM_TOTA_BASY,2)  
   End      
  Else  
   Begin  
     Select @VNIM_AFEC_ORIX = Round(@VNIM_TOTA_BASX,2)  
     Select @VNIM_INAF_ORIX = 0.00  
   End      
  If @VSCO_MONE != @VSCO_MONE_BAS1  
   Begin  
     Select @VNIM_IIGV_ORIX = @VNIM_IIGV_ORIG  
     Select @VNIM_FONA_ORIX = @VNIM_FONA_ORIG  
     Select @VNIM_RENT_ORIX = @VNIM_RENT_ORIG  
     Select @VNIM_TOTA_ORIX = @VNIM_TOTA_ORIG  
     Select @VNIM_AFEC_SOLX = Round(@VNIM_AFEC_ORIX * @VNFA_CAMB,2)  
     Select @VNIM_INAF_SOLX = Round(@VNIM_INAF_ORIX * @VNFA_CAMB,2)  
  
     Select @VNIM_IIGV_SOLX = Round(@VNIM_IIGV_ORIG * @VNFA_CAMB,2)  
     Select @VNIM_FONA_SOLX = Round(@VNIM_FONA_ORIG * @VNFA_CAMB,2)  
     Select @VNIM_RENT_SOLX = Round(@VNIM_RENT_ORIG * @VNFA_CAMB,2)  
     Select @VNIM_TOTA_SOLX = Round(@VNIM_TOTA_ORIG * @VNFA_CAMB,2)  
   End  
  Else  
   Begin  
     Select @VNIM_AFEC_SOLX = Round(@VNIM_AFEC_ORIX,2)  
     Select @VNIM_INAF_SOLX = Round(@VNIM_INAF_ORIX,2)  
     Select @VNIM_IIGV_SOLX = Round(@VNIM_IIGV_ORIG,2)  
     Select @VNIM_FONA_SOLX = Round(@VNIM_FONA_ORIG,2)  
     Select @VNIM_RENT_SOLX = Round(@VNIM_RENT_ORIG,2)  
     Select @VNIM_TOTA_SOLX = Round(@VNIM_TOTA_ORIG,2)  
     Select @VNIM_AFEC_ORIX = Null  
     Select @VNIM_INAF_ORIX = Null  
     Select @VNIM_IIGV_ORIX = Null  
     Select @VNIM_FONA_ORIX = Null  
     Select @VNIM_RENT_ORIX = Null  
     Select @VNIM_TOTA_ORIX = Null  
     Select @VNFA_CAMB      = Null  
   End  
   Select @VSCO_SUNA = CO_SUNA From TTDOCU_CNTB  
    Where TI_DOCU = @VSTI_DOCU_CNTB  
     
   Select @VSTI_DOCU_AUXI = @VSTI_DOCU_CNTB  
     
   Insert Into #TWMVTO_CNTB_Q22_1 Values  
     (@VDFE_DOCU, @VSCO_SUNA, @VSTI_DOCU_AUXI, @VSNU_DOCU, @VSCO_UNID,  
      @VSCO_OPRC, @VSNS_ASTO, @VSCO_PROV, @VSNO_CORT_PROV, @VSNU_RUCS_PROV, @VSCO_MONE,  
      @VNIM_INAF_ORIX, @VNIM_AFEC_ORIX, @VNIM_IIGV_ORIX, @VNIM_FONA_ORIX,  
      @VNIM_RENT_ORIX, @VNIM_TOTA_ORIX, @VNIM_INAF_SOLX, @VNIM_AFEC_SOLX,   
@VNIM_IIGV_SOLX, @VNIM_FONA_SOLX, @VNIM_RENT_SOLX, @VNIM_TOTA_SOLX,  
      @VDFE_DOCU_REFE, @VSTI_DOCU_REFE, @VSNU_DOCU_REFE, @VNFA_CAMB)  
  End  
  
  
/*-- Borro las tablas fijas ---*/  
delete X61COAPR  
delete X58CPCOA  
delete X68CPCOA  
delete X78CPCOA  
  
/*-- Borro los demas ---*/  
delete #TWMVTO_CNTB_Q22_1  
 where TI_DOCU_SUNA NOT IN ('01', '07', '08', '50')  
  
/*---- Cursor para el COA ----*/  
Declare CU_TXMVTO_CNTB_Q22_2 Cursor  
   For Select * from #TWMVTO_CNTB_Q22_1  
  
Open CU_TXMVTO_CNTB_Q22_2  
  
Fetch CU_TXMVTO_CNTB_Q22_2 Into @CDFE_DOCU, @CSTI_DOCU_SUNA, @CSTI_DOCU_CNTB, @CSNU_DOCU,  
 @CSCO_UNID_RGST, @CSCO_OPRC, @CSNS_ASTO, @CSCO_PROV, @CSNO_CORT_PROV, @CSNU_RUCS_PROV,  
 @CSCO_MONE, @CNIM_INAF_ORIG, @CNIM_AFEC_ORIG, @CNIM_IIGV_ORIG, @CNIM_FONA_ORIG,  
 @CNIM_RENT_ORIG, @CNIM_TOTA_ORIG, @CNIM_INAF_SOLE, @CNIM_AFEC_SOLE, @CNIM_IIGV_SOLE,  
 @CNIM_FONA_SOLE, @CNIM_RENT_SOLE, @CNIM_TOTA_SOLE, @CDFE_DOCU_REFE, @CSTI_DOCU_REFE,   
        @CSNU_DOCU_REFE, @CNFA_CAMB  
  
While @@Fetch_Status = 0  
 Begin  
  
      Select @VNIM_AFEC_SOLE = (@CNIM_INAF_SOLE + @CNIM_AFEC_SOLE)  
      Select @VNIM_IIGV_SOLE = @CNIM_IIGV_SOLE  
   
   /*----------------  
      If @CNIM_OPER_GRA1 != 0  
        Select @VSDE_CODI_ADQU = '001'  
   
      If @CNIM_OPER_COM1 != 0  
        Select @VSDE_CODI_ADQU = '002'  
  
      If @CNIM_COMP_NUTI != 0  
        Select @VSDE_CODI_ADQU = '003'  
           
      If @CNIM_COMP_INAF != 0  
        Select @VSDE_CODI_ADQU = '004'  
    -------------------*/   
     
      If @VNIM_AFEC_SOLE < 0  
         Select @VNIM_AFEC_SOLE = ( @VNIM_AFEC_SOLE * - 1 )  
  
      If @VNIM_IIGV_SOLE < 0  
         Select @VNIM_IIGV_SOLE = ( @VNIM_IIGV_SOLE * - 1 )  
  
      Select @VSNU_ORDE = substring(@CSNU_ASTO, 5, 6)  
  
      Select @VSDE_MESE = Convert(Char(2),@INNU_MESE)  
      Select @VSDE_ANNO = Convert(Char(4),@INNU_ANNO)  
   
      If Datalength(Rtrim(@VSDE_MESE)) = 1  
         Select @VSDE_MESE = '0' + @VSDE_MESE  
  
      Select @VSDE_PERI = @VSDE_ANNO + @VSDE_MESE  
  
      Select @VNNU_ANNO_DOCU = Datepart(year,@CDFE_DOCU)  
      Select @VNNU_MESE_DOCU = Datepart(month,@CDFE_DOCU)  
      Select @VNNU_DIAS_DOCU = Datepart(day,@CDFE_DOCU)  
      Select @VSDE_ANNO_DOCU = Convert(Char(04),@VNNU_ANNO_DOCU)  
      Select @VSDE_MESE_DOCU = Convert(Char(02),@VNNU_MESE_DOCU)  
      Select @VSDE_DIAS_DOCU = Convert(Char(02),@VNNU_DIAS_DOCU)  
  
      If Datalength(Rtrim(@VSDE_DIAS_DOCU)) = 1  
          Select @VSDE_DIAS_DOCU = '0'+ @VSDE_DIAS_DOCU  
  
      If Datalength(Rtrim(@VSDE_MESE_DOCU)) = 1  
          Select @VSDE_MESE_DOCU = '0'+ @VSDE_MESE_DOCU  
  
      Select @VSDE_FECH_DOCU = Rtrim(@VSDE_ANNO_DOCU) + Rtrim(@VSDE_MESE_DOCU) + Rtrim(@VSDE_DIAS_DOCU)  
----------------------  
Select @VSDE_FECH_DOCU  
      if @CSTI_DOCU_SUNA = '50'  
         Select @VSNU_DOCU = @CSNU_DOCU  
  
      Else  
       Begin     
          Select @VNNU_POSI_SERI = charindex('-', @CSNU_DOCU)  
          if @VNNU_POSI_SERI = 0  
           Begin  
             Select @VSNU_SERI = NULL  
             Select @VSNU_DOCU = @CSNU_DOCU  
           End  
          Else  
           Begin  
             Select @VSNU_SERI = substring(@CSNU_DOCU, 1, @VNNU_POSI_SERI - 1)  
             Select @VSNU_DOCU = substring(@CSNU_DOCU, @VNNU_POSI_SERI + 1, 20)  
           End   
       End  
    
    if @CSTI_DOCU_SUNA = '07' or @CSTI_DOCU_SUNA = '08'  
     Begin   
      Select @VNNU_ANNO_REFE = Datepart(year,@CDFE_DOCU_REFE)  
      Select @VNNU_MESE_REFE = Datepart(month,@CDFE_DOCU_REFE)  
      Select @VNNU_DIAS_REFE = Datepart(day,@CDFE_DOCU_REFE)  
      Select @VSDE_ANNO_REFE = Convert(Char(04),@VNNU_ANNO_REFE)  
      Select @VSDE_MESE_REFE = Convert(Char(02),@VNNU_MESE_REFE)   
  
      If Datalength(Rtrim(@VSDE_DIAS_REFE)) = 1  
          Select @VSDE_DIAS_REFE = '0'+ @VSDE_DIAS_REFE  
  
      If Datalength(Rtrim(@VSDE_MESE_REFE)) = 1  
          Select @VSDE_MESE_REFE = '0'+ @VSDE_MESE_REFE  
  
      Select @VSDE_FECH_REFE = Rtrim(@VSDE_ANNO_REFE) + Rtrim(@VSDE_MESE_REFE) + Rtrim(@VSDE_DIAS_REFE)  
----------------------  
Select @VSDE_FECH_REFE  
      Select @VSTI_SUNA_REFE = CO_SUNA  
    from TTDOCU_CNTB  
        where TI_DOCU = @CSTI_DOCU_REFE      
  
      Select @VNNU_POSI_REFE = charindex('-', @CSNU_DOCU_REFE)  
      if @VNNU_POSI_REFE = 0  
        Begin  
   Select @VSNU_SERI_REFE = NULL  
         Select @VSNU_DOCU_REFE = @CSNU_DOCU_REFE  
        End  
      Else  
        Begin  
         Select @VSNU_SERI_REFE = substring(@CSNU_DOCU_REFE, 1, @VNNU_POSI_REFE - 1)  
         Select @VSNU_DOCU_REFE = substring(@CSNU_DOCU_REFE, @VNNU_POSI_REFE + 1, 20)  
        End   
     End  
select 'entra a insert      X58CPCOA'        
     if @CSTI_DOCU_SUNA = '01'  
      Begin  
        Select @VNNU_SECU_FACT = @VNNU_SECU_FACT + 1   
select 'inserta      X58CPCOA'  
select @VNNU_SECU_FACT, @CSNU_RUCS_PROV, @VSDE_PERI, @VSDE_FECH_DOCU, @CSTI_DOCU_SUNA,  
                              @VSNU_SERI, @VSNU_DOCU, @VNIM_AFEC_SOLE, @VNIM_IIGV_SOLE, @VSDE_CODI_ADQU,  
         '1', @VSNU_ORDE, '0'  
        Insert Into X58CPCOA (X58NROREG, X58IDENT, X58PERIODO, X58FECHA, X58TIPO,  
         X58SERIE, X58NUMERO, X58BASE, X58IGV, X58CODIGO,  
                        X58FLAGMON, X58NORDEN, X58ESTADO )  
         Values(@VNNU_SECU_FACT, @CSNU_RUCS_PROV, @VSDE_PERI, @VSDE_FECH_DOCU, @CSTI_DOCU_SUNA,  
                              @VSNU_SERI, @VSNU_DOCU, @VNIM_AFEC_SOLE, @VNIM_IIGV_SOLE, '001',  
         '1', '000001', '0' )  
         
      End  
  
     if @CSTI_DOCU_SUNA = '07' or @CSTI_DOCU_SUNA = '08'  
       Begin  
         Select @VNNU_SECU_NOTA = @VNNU_SECU_NOTA + 1  
  
        Insert Into X68CPCOA (X68NROREG, X68IDENT, X68PERIODO, X68FECHA, X68TIPO,  
                       X68SERIE, X68NUMERO, X68BASE, X68IGV, X68CODIGO,  
                        X68FLAGMON, X68NORDEN, X68TIPORI, X68SERORI, X68NUMORI,   
                        X68FECORI, X68ESTADO )  
  
         Values(@VNNU_SECU_NOTA, @CSNU_RUCS_PROV, @VSDE_PERI, @VSDE_FECH_DOCU, @CSTI_DOCU_SUNA,   
     @VSNU_SERI, @VSNU_DOCU, @VNIM_AFEC_SOLE, @VNIM_IIGV_SOLE, @VSDE_CODI_ADQU,   
     '1', @VSNU_ORDE, @VSTI_SUNA_REFE, @VSNU_SERI_REFE, @VSNU_DOCU_REFE,   
  
      @VSDE_FECH_REFE, '0' )  
       End      
  
     if @CSTI_DOCU_SUNA = '50'  
       Begin  
         Select @VNNU_SECU_POLI = @VNNU_SECU_POLI + 1  
  
         Insert Into X78CPCOA (X78NROREG, X78PERIODO, X78F_PAGO, X78TIPO, X78NUMPOL,  
          X78NUMFRAC, X78NUMCUO, X78M_IMPOR, X78ADVALOR, X78ISC,  
                  X78IGV, X78CODIGO, X78NORDEN, X78ESTADO)  
          Values(@VNNU_SECU_POLI, @VSDE_PERI, @VSDE_FECH_DOCU, @CSTI_DOCU_SUNA, @VSNU_DOCU,   
          ' ', 0, @VNIM_AFEC_SOLE, 0 ,0,   
                    @VNIM_IIGV_SOLE, @VSDE_CODI_ADQU, @VSNU_ORDE, '0')  
       End  
  
    if NOT Exists(Select * from X61COAPR  
    where X61NRORUC = @CSNU_RUCS_PROV)  
     Begin  
        Select @VNNU_SECU_PROV = @VNNU_SECU_PROV + 1   
      
        Insert Into X61COAPR (X61NROREG, X61NRORUC, X61CINTER, X61NOMBRE, X61ESTADO )  
        Values(@VNNU_SECU_PROV, @CSNU_RUCS_PROV, @CSCO_PROV, @CSNO_CORT_PROV, '0')  
     End  
   
  
  
  Fetch CU_TXMVTO_CNTB_Q22_2 into @CDFE_DOCU, @CSTI_DOCU_SUNA, @CSTI_DOCU_CNTB, @CSNU_DOCU,  
 @CSCO_UNID_RGST, @CSCO_OPRC, @CSNS_ASTO, @CSCO_PROV, @CSNO_CORT_PROV, @CSNU_RUCS_PROV,  
 @CSCO_MONE, @CNIM_INAF_ORIG, @CNIM_AFEC_ORIG, @CNIM_IIGV_ORIG, @CNIM_FONA_ORIG,  
 @CNIM_RENT_ORIG, @CNIM_TOTA_ORIG, @CNIM_INAF_SOLE, @CNIM_AFEC_SOLE, @CNIM_IIGV_SOLE,  
 @CNIM_FONA_SOLE, @CNIM_RENT_SOLE, @CNIM_TOTA_SOLE, @CDFE_DOCU_REFE, @CSTI_DOCU_REFE,  
        @CSNU_DOCU_REFE, @CNFA_CAMB  
 End  
  
 close CU_TXMVTO_CNTB_Q22_2   
 deallocate CU_TXMVTO_CNTB_Q22_2  
  
 Return  
/*--------------------------------- Fin -------------------------------------*/  
  
  
  
  
  
  
  
  
  
  
  
  
  






GO
